//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.trt.eatmeat.sign;

import java.security.MessageDigest;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

public final class SignUtils {
    public SignUtils() {
    }

    public static String createSign(String secret, Map<String, String> params) {
        Set<String> sortedParams = new TreeSet();
        sortedParams.addAll(params.keySet());
        StringBuilder strB = new StringBuilder();
        Iterator var4 = sortedParams.iterator();

        String key;
        while(var4.hasNext()) {
            key = (String)var4.next();
            if (!key.equalsIgnoreCase("sign")) {
                String value = (String)params.get(key);
                if (value != null && !value.isEmpty()) {
                    strB.append(key).append(value);
                }
            }
        }

        key = secret + strB.toString();
        return createSign(key);
    }

    private static String createSign(String str) {
        if (str != null && str.length() != 0) {
            char[] hexDigits = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

            try {
                MessageDigest mdTemp = MessageDigest.getInstance("SHA1");
                mdTemp.update(str.getBytes("UTF-8"));
                byte[] md = mdTemp.digest();
                int j = md.length;
                char[] buf = new char[j * 2];
                int k = 0;

                for(int i = 0; i < j; ++i) {
                    byte byte0 = md[i];
                    buf[k++] = hexDigits[byte0 >>> 4 & 15];
                    buf[k++] = hexDigits[byte0 & 15];
                }

                return new String(buf);
            } catch (Exception var9) {
                return null;
            }
        } else {
            return null;
        }
    }

    public static boolean checkSign(String secret, Map<String, String> params) {
        return params.containsKey("sign") && ((String)params.get("sign")).equals(createSign(secret, params));
    }
}
